﻿#region License
/*
 *CopyRight (c) By Denuvead All Rights Reserved
 *CreateDate:2012-11-17 01:02
 *Info:EnumExtensions
 */
#endregion

using System;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;

namespace PriErp.Tools.Extensions
{
    /// <summary>
    /// 扩展类
    /// </summary>
    public static partial class PriErpExtension
    {
        /// <summary>
        /// 字符串转换为指定枚举(区分大小写)
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="sVal">需转换的字符串</param>
        /// <param name="dVal">默认值</param>
        /// <returns>返回指定枚举类型</returns>
        static public T ToEnum<T>(this string sVal, T dVal)
        {
            Type type = typeof(T);
            if (!type.IsEnum)
                throw new ArgumentException("Not Enum!!!");
            T result = dVal;
            if (!string.IsNullOrEmpty(sVal) && Enum.IsDefined(type, sVal))
            {
                result = (T)Enum.Parse(type, sVal);
            }
            return result;
        }

        /// <summary>
        /// 字符串转换为指定枚举(默认值为default(T)）
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="sVal">需转换的字符串</param>
        /// <returns>返回指定枚举类型</returns>
        static public T ToEnum<T>(this string sVal)
        {
            return sVal.ToEnum<T>(default(T));
        }

        /// <summary>
        /// 获取枚举的描述信息
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(this Enum value)
        {
            Type type = value.GetType();
            return Utils.GetDescription(type, value.ToString());
        }
    }
}
